package com.ymatou.iapi.productspu.testcase;

import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productspu.parameter.PassBean;
import com.ymatou.iapi.productspu.service.PassCall;
import com.ymatou.iapi.productspu.service.PspSpuSyncCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.sqlwapper.SpuPropertyWapper;
import com.ymttest.utils.TestDataManager;
import org.apache.http.client.ClientProtocolException;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.io.IOException;
import java.util.*;
/**
*执行同步指令
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_PspSpuSync{
	private static PspSpuSyncCall PspSpuSyncCall;
	private static SpuPropertyWapper SpuPropertyWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("执行同步指令");
	}

	@Before
	public void caseUp() {
		PspSpuSyncCall=new PspSpuSyncCall();
		SpuPropertyWapper=new SpuPropertyWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(PspSpuSyncCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_PspSpuSync_001() {
		Logger.start(true,"执行同步指令成功");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
			addpro.getProduct().setProductName(TestDataManager.getChineseName()+TestDataManager.getRandomUUID());
			String ProductId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();

			Thread.sleep(2000);
			SpuPropertyWapper SpuPropertyWapper=new SpuPropertyWapper();
			Map spuinfo = SpuPropertyWapper.selectSpuProductInfoByProductId(ProductId);
			Logger.debug(spuinfo);
			Integer spuId=(Integer) spuinfo.get("SpuId");
			PassBean passBean=new PassBean();
			PassCall passCall=new PassCall();
			passBean.setAuditor("autoTest");
			passBean.setSPUId(spuId);
			passCall.setData(passBean);
			passCall.callService();
			Logger.verifyEquals("200", passCall.getString("Status"), "验证Status");
			
			PspSpuSyncCall.callService();
			Logger.verifyIsContains("ok", PspSpuSyncCall.getReturnData(), "验证返回");
			verify(ProductId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_PspSpuSync_002() {
		Logger.start(true,"指定pid执行同步指令成功");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
			addpro.getProduct().setProductName(TestDataManager.getChineseName()+TestDataManager.getRandomUUID());
			String ProductId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			SpuPropertyWapper SpuPropertyWapper=new SpuPropertyWapper();
			PspSpuSyncCall.setData("ProductId="+ProductId);
			PspSpuSyncCall.callService();
			Logger.verifyIsContains("ok", PspSpuSyncCall.getReturnData(), "验证返回");
			verify(ProductId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_PspSpuSync_003() {
		Logger.start(true,"多次执行指定pid执行同步指令成功");
		try {
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
			addpro.getProduct().setProductName(TestDataManager.getChineseName()+TestDataManager.getRandomUUID());
			String ProductId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			SpuPropertyWapper SpuPropertyWapper=new SpuPropertyWapper();
			PspSpuSyncCall.setData("ProductId="+ProductId);
			PspSpuSyncCall.callService();
			Logger.verifyIsContains("ok", PspSpuSyncCall.getReturnData(), "验证返回");
			
			
			AddSellerProductRequest addpro2 = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
			addpro2.getProduct().setProductName(TestDataManager.getChineseName()+TestDataManager.getRandomUUID());
			String ProductId2 = ProductManagerCallServiceV2.addNewProduct(addpro2).get("Data").getAsJsonObject().get("ProductId").getAsString();
			PspSpuSyncCall.setData("ProductId="+ProductId2);
			PspSpuSyncCall.callService();
			Logger.verifyIsContains("ok", PspSpuSyncCall.getReturnData(), "验证返回");
			
			verify(ProductId);
			verify(ProductId2);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void verify(String productId) throws ClientProtocolException, IOException, InterruptedException {
		Logger.comment("验证总线发送消息");
		MessageListBean MessageListBean = MqQueryCallService.initPspSpuSync(productId);
		List <Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
		int redo=0;
		while(joa.size()==0&&redo<5){
			redo++;
			Thread.sleep(5000);
			 joa = MqQueryCallService.GetMqMessage(MessageListBean);
		}
		Logger.verifyEquals(1, joa.size(), "验证发送总线消息");
		Logger.verifyEquals(productId,joa.get(0).get("productId").toString(), "验证ProductId");
		Thread.sleep(5000);
		Map spuinfo = SpuPropertyWapper.selectSpuProductInfoByProductId(productId);
		Integer status = (Integer) spuinfo.get("Status");
		Logger.verifyEquals(-2, status, "验证status为-2");
	}

}